<template>
  <div class="upload-container">
    <el-upload ref="upload"
               :data="dataObj"
               :multiple="false"
               :auto-upload="false"
               :show-file-list="false"
               :on-success="handleImageSuccess"
               :on-change="handImgChange"
               class="image-uploader"
               drag
               action="https://httpbin.org/post">
      <i class="el-icon-upload" />
      <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
    </el-upload>
    <div class="image-preview image-app-preview">
      <div v-show="imageUrl.length > 1"
           class="image-preview-wrapper">
        <img :src="imageUrl">
        <div class="image-preview-action">
          <i class="el-icon-delete"
             @click="rmImage" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getImageFileFromUrl } from '@/utils'
export default {
  name: 'SingleImageUpload3',
  props: {
    value: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      tempUrl: '',
      dataObj: { token: '', key: '' }
    }
  },
  computed: {
    imageUrl () {
      // 图片回显（编辑时后台返回的网络地址）
      if (this.value.startsWith('http')) {
        getImageFileFromUrl(this.value, '原图.png').then((imgFile) => {
          this.$emit('update:file', imgFile)
        }).catch(() => { })
      }
      // 本地选择后 此处为图片base64数据
      return this.value
    }
  },
  methods: {
    // 移除文件
    rmImage () {
      this.emitInput('')
    },
    emitInput (val) {
      // 此处传递的是文件预览图
      this.$emit('input', val)
    },
    // 文件上传成功
    handleImageSuccess (file) {
      this.emitInput(file.files.file)
    },
    // 文件状态改变（选择文件、文件上传成功｜失败）
    handImgChange ({ raw: file }) {
      this.uploadImg(file)
      const reader = new FileReader()
      reader.readAsDataURL(file)
      reader.onload = () => {
        this.emitInput(reader.result)
      }
      // 此处传递的是文件对象（File）
      // this.$emit("update:file", file);
    },

    uploadImg: function (file) {
      const _that = this
      const forms = new FormData()
      forms.append('file', file)
      forms.append('app_key', 'weilai')
      forms.append('source_type', '2')
      this.$http.post(`https://files.meta-720.com/upload/v1/`, forms)
        .then(({ data }) => {
          const { source_path } = data.files[0]
          console.log(source_path)
          _that.$emit('update:file', source_path)
        }).catch(() => {
        })
    },

    // 自定义文件上传
    beforeUpload () { }
  }
}
</script>

<style lang="scss" scoped>
@import '~@/styles/mixin.scss';
.upload-container {
  width: 100%;
  position: relative;
  display: flex;
  align-items: center;
  .image-uploader {
    width: 35%;
  }
  .image-preview {
    width: 200px;
    height: 200px;
    position: relative;
    border: 1px dashed #d9d9d9;
    float: left;
    margin-left: 50px;
    .image-preview-wrapper {
      position: relative;
      width: 100%;
      height: 100%;
      img {
        width: 100%;
        height: 100%;
        object-fit: contain;
      }
    }
    .image-preview-action {
      position: absolute;
      width: 100%;
      height: 100%;
      left: 0;
      top: 0;
      cursor: default;
      text-align: center;
      color: #fff;
      opacity: 0;
      font-size: 20px;
      background-color: rgba(0, 0, 0, 0.5);
      transition: opacity 0.3s;
      cursor: pointer;
      text-align: center;
      line-height: 200px;
      .el-icon-delete {
        font-size: 36px;
      }
    }
    &:hover {
      .image-preview-action {
        opacity: 1;
      }
    }
  }
  .image-app-preview {
    width: 320px;
    height: 180px;
    position: relative;
    border: 1px dashed #d9d9d9;
    float: left;
    margin-left: 50px;
    .app-fake-conver {
      height: 44px;
      position: absolute;
      width: 100%; // background: rgba(0, 0, 0, .1);
      text-align: center;
      line-height: 64px;
      color: #fff;
    }
  }
}
</style>
